

/****************************************************
// 功能：英雄单位
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/28 17:7:37
*****************************************************/

public class Hero : MainLogicUnit
{
    public int heroId;
    public int posIndex;
    public string userName;
    
    private bool setRevive;
    
    public Hero(HeroData hd) : base(hd)
    {
        heroId = hd.heroId;
        posIndex = hd.posIndex;
        userName = hd.userName;

        unitType = UnitTypeEnum.Hero;
        unitName = $"{unitData.unitConfig.unitName}_{userName}";
        pathPrefix = "Chars";
    }

    public override void LogicTick()
    {
        base.LogicTick();

        if (unitState == UnitStateEnum.Dead && setRevive == false)
        {
            setRevive = true;
            // 更新击杀数据
            BattleController.Instance.SetKillData(IsTeam(TeamEnum.Blue) ? TeamEnum.Red : TeamEnum.Blue);

            if (IsPlaySelf())
            {
                BattleController.Instance.SetReviveTime(true, 5);
            }
            
            CreateLogicTimer(() =>
            {
                setRevive = false;
                
                if (IsPlaySelf()) { BattleController.Instance.SetReviveTime(false); }
                unitState = UnitStateEnum.Alive;

                isDirChanged = true;
                LogicPos = unitData.bornPos;
                    
                mainViewUnit.ForcePosSync();
                ResetHP();
            },5000);
        }
    }

    #region API Functions

    public override bool IsPlaySelf()
    {
        return posIndex == ModelManager.Instance.currentIndex;
    }

    public override bool Equals(MainLogicUnit mainLogicUnit)
    {
        if (mainLogicUnit.unitType != unitType) return false;
        var hero = mainLogicUnit as Hero;
        return hero != null && hero.posIndex == posIndex;
    }

    #endregion
    
}
